package com.itheima.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.goods.mapper.CategoryDao;
import com.itheima.goods.pojo.Category;
import com.itheima.goods.service.CategoryService;
import entity.PageResult;
import entity.Result;

import entity.StatusCode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao,Category> implements CategoryService {

    /**
     * 查询全部分类信息
     * @return
     */
    @Override
    public Result categoryCare() {
        //MP查询
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.ge("id", 1); //查询大于等于1，相当于查询全部
        List<Category> categoryList = baseMapper.selectList(categoryQueryWrapper);

        return new Result(true, StatusCode.OK, "查询成功", categoryList);
    }

    /**
     * 添加一个分类信息
     * @param category
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result addCategory(Category category) {
        Integer count = baseMapper.insert(category);
        return new Result(true,StatusCode.OK,"添加成功",count);
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Result findByPage(Integer page, Integer size) {
        //MP实现分页查询
        IPage<Category> iPage = new Page<>(page, size);
        page(iPage);
        PageResult pageList = new PageResult(iPage.getTotal(), iPage.getRecords());
        return new Result(true,StatusCode.OK,"查询成功",pageList);
    }

    /**
     * 根据id查询分类信息
     * @param id
     * @return
     */
    @Override
    public Result findById(Integer id) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        List<Category> list = baseMapper.selectList(wrapper);
        return new Result(true, StatusCode.OK,"查询成功",list);

    }

    /**
     * 更新分类信息
     * @param id
     * @param categories
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result updateCategoryById(Integer id,Category categories) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        Integer count = baseMapper.update(categories, wrapper);
        return new Result(true, StatusCode.OK,"修改成功",count);
    }


    @Override
    public Result delete(Integer id) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        int delete = baseMapper.delete(wrapper);
        return new Result(true,StatusCode.OK,"删除成功",delete);
    }


}
